// pages/home/index.js
var util = require('../../utils/util.js');
Component({

  /**
   * 页面的初始数据
   */
  data: {
    home: [],
    offset: 0,
    limit: 10,
    toBePaid: [1],
    toBeTravel: [],
    toBeEvaluated: [],
    colors:"colors",
    click:1,
    show:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  ready: function (options) {
    if (!util.isLogin()) {
      wx.showToast({
        title: '未登录，请登录！',
        icon: 'none',
        duration: 2000
      })
      this.triggerEvent('index');
     
      // wx.redirectTo({
      //   url: '/pages/index/index?id=4'
      // })
      return;
    }
    this.init();
  },

// 点击事件










  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      this.init();
     },
  },

  methods: {
    init() {
      var that = this
      this.getItinerary(this.getOpt(0), function(data){
       
        that.setData({
          toBePaid: data,
          show:data.length
        })
      })
      this.getItinerary(this.getOpt(3), function (data) {
        that.setData({
          toBeTravel: data,
          show: data.length
        })
      })
      this.getItinerary(this.getOpt(5), function (data) {
        that.setData({
          toBeEvaluated: data,
          show: data.length
        })
      })
      this.getItinerary(this.getOpt(2), function (data) {
        that.setData({
          tofail: data,
          show: data.length
        })
      })
    },
    getItinerary (options, callback) {
      let defaultData = {
        user_id: util.isLogin().id,
        offset: this.data.offset,
        limit: this.data.limit,
      }
      util.http({
        url: 'api/miniwechat/center/itinerary',
        data: options || defaultData
      }).then((data) => {
        callback && callback(data)
      }).catch((err) => {
        wx.showToast({
          title: String(err),
          icon: 'none',
          duration: 2000
        })
      })
    },
    getOpt(status) {
      let options = {
        user_id: util.isLogin().id,
        offset: 0,
        limit: 5,
        status: status
      }
      return options
    },
    goMoreConent(e) {
      var status = e.currentTarget.dataset.status
      var title = e.currentTarget.dataset.title
      wx.navigateTo({
        url: '/pages/moreContent/index?status=' + status + '&title=' + title
      })
    },

    // 头部点击事件
    clicktab(e){
      console.log(e.currentTarget.dataset.click)
      // 进行判断
      if (e.currentTarget.dataset.click==1){
        this.setData({
          click:1
        })
      }
      if (e.currentTarget.dataset.click == 2) {
        this.setData({
          click:2
        })
      }
      if (e.currentTarget.dataset.click == 3) {
        this.setData({
          click:3
        })
      }
    },


    // 
    goOrderDetail(e) {
      let data = e.currentTarget.dataset
      wx.navigateTo({
        url: '/pages/orderDetail/index?id=' + data.id
      })
    },
    goPaid(e) {
      console.log(e)
      let data = e.currentTarget.dataset;
      if (data.people > 0) {
        util.http({
          url: 'api/miniwechat/order/pay',
          method: 'POST',
          isPay: true,
          data: {
            out_trade_no: e.currentTarget.dataset.num,
            source: 2
          }
        }).then(res => {
          wx.requestPayment({
            timeStamp: String(res.timeStamp),
            nonceStr: String(res.nonceStr),
            package: String(res.package),
            signType: String(res.signType),
            paySign: String(res.paySign),
            success(resx) {
              wx.redirectTo({
                url: '/pages/paySuccess/index?state=1' + '&orderId=' + e.currentTarget.dataset.orderid
              })
            },
            fail(res) {
              wx.redirectTo({
                url: '/pages/paySuccess/index?state=0'
              })
            }
          })
        })
      } else {
        wx.setStorage({
          key: 'orderInfo',
          data: {
            user_id: util.isLogin().user_id,
            line_id: data.id,
            line_time_id: data.timeid,
            out_trade_no: data.num
          }
        })
        wx.navigateTo({
          url: '/pages/firmOrder/index?source=2',
        })
      }
    },
    cancelOrder (e) {
      let orderId = e.currentTarget.dataset.id
      let orderNo = e.currentTarget.dataset.num
      let that = this
      wx.showModal({
        title: '',
        content: '确认取消订单吗？',
        confirmColor: '#E50164',
        success(res) {
          if (res.confirm) {
            util.http({
              url: 'api/miniwechat/center/cancel_itinerary',
              method: 'POST',
              data: {
                user_id: util.isLogin().id,
                order_id: orderId,
                out_trade_no: orderNo
              }
            }).then(res => {
              that.init();
              wx.showToast({
                title: '取消成功',
                icon: 'none',
                duration: 2000
              })
            }).catch(err => {
              wx.showToast({
                title: '取消失败',
                icon: 'none',
                duration: 2000
              })
            })
          } else if (res.cancel) {
            console.log('取消')
          }
        }
      })
    },
    toRefund (e) {
      wx.navigateTo({
        url: '/pages/applyRefund/index?id='+e.currentTarget.dataset.id,
      })
    },
    goRouteDetail(e) {
      wx.navigateTo({
        url: '/pages/routeDetail/index?id=' + e.currentTarget.dataset.id,
      })
    },
    toComplete(e) {
      let orderId = e.currentTarget.dataset.id
      let time = e.currentTarget.dataset.time ? new Date(e.currentTarget.dataset.time) : '';
      let that = this;
      console.log(time)
      if (new Date() < time) {
        wx.showModal({
          title: '',
          content: '线路未出行完毕，点击“确认”后，款项将支付给商家，请确定',
          confirmColor: '#E50164',
          success(res) {
            if (res.confirm) {
              util.http({
                url: 'api/miniwechat/order/order_ful',
                method: 'POST',
                data: {
                  user_id: util.isLogin().id,
                  order_id: orderId,
                }
              }).then(res => {
                that.init();
                wx.showToast({
                  title: '确认成功',
                  icon: 'none',
                  duration: 2000
                })
              }).catch(err => {
                wx.showToast({
                  title: '确认失败',
                  icon: 'none',
                  duration: 2000
                })
              })
            } else if (res.cancel) {
              console.log('取消')
            }
          }
        })
        return;
      }
      wx.showModal({
        title: '',
        content: '确认已出行？',
        confirmColor: '#E50164',
        success(res) {
          if (res.confirm) {
            util.http({
              url: 'api/miniwechat/order/order_ful',
              method: 'POST',
              data: {
                user_id: util.isLogin().id,
                order_id: orderId,
              }
            }).then(res => {
              that.init();
              wx.showToast({
                title: '确认成功',
                icon: 'none',
                duration: 2000
              })
            }).catch(err => {
              wx.showToast({
                title: '确认失败',
                icon: 'none',
                duration: 2000
              })
            })
          } else if (res.cancel) {
            console.log('取消')
          }
        }
      })
    },
    toComment(e) {
      wx.navigateTo({
        url: '/pages/comment/index?lineId=' + e.currentTarget.dataset.lineid + '&orderId=' + e.currentTarget.dataset.orderid,
      })
    }
  }
})